﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using Asah.Web.Encryption;

namespace Asah.SiteWorx.Commerce
{
    [Serializable()]
    public class PaymentDetailInfo : Csla.ReadOnlyBase<PaymentDetailInfo>
    {
        private int _orderId;
        private PaymentMethod _paymentMethod;
        private string _nameOnCard;
        private string _cardNumber;
        private string _expirationMonth;
        private string _expirationYear;

        public int OrderId
        {
            get
            {
                return _orderId;
            }
            internal set
            {
                _orderId = value;
            }
        }

        public PaymentMethod PaymentMethod
        {
            get
            {
                return _paymentMethod;
            }
            internal set
            {
                _paymentMethod = value;
            }
        }

        public string NameOnCard
        {
            get
            {
                return _nameOnCard;
            }
            internal set
            {
                _nameOnCard = value;
            }
        }

        public string ExpirationMonth
        {
            get
            {
                return _expirationMonth;
            }
            internal set
            {
                _expirationMonth = value;
            }
        }

        public string ExpirationYear
        {
            get
            {
                return _expirationYear;
            }
            internal set
            {
                _expirationYear = value;
            }
        }

        public string GetCardNumber()
        {
            Symmetric crypto = new Symmetric(Symmetric.Provider.Rijndael);
            crypto.Key.Text = ";]ó”(¨Mêy¸¡ƒÜµ´…I£ªšÀð$g¬~c*";
            string cardNumber = crypto.Decrypt(new Data(_cardNumber.FromBase64())).Text;

            cardNumber = cardNumber.Replace("-", " ");

            if (cardNumber.Length == 16)
                cardNumber = cardNumber.Substring(0, 4) + " " + cardNumber.Substring(4, 4) + " " +
                             cardNumber.Substring(8, 4) + " " + cardNumber.Substring(12, 4);
            return cardNumber;
        }

        public override string ToString()
        {
            return _orderId.ToString();
        }

        protected override object GetIdValue()
        {
            return _orderId;
        }

        internal PaymentDetailInfo()
        {
            // require use of factory methods
        }

        internal PaymentDetailInfo(int orderId, PaymentMethod paymentMethod, string nameOnCard, string cardNumber,
                                   string expirationMonth, string expirationYear)
        {
            _orderId = orderId;
            _paymentMethod = paymentMethod;
            _nameOnCard = nameOnCard;
            _cardNumber = cardNumber;
            _expirationMonth = expirationMonth;
            _expirationYear = expirationYear;
        }
    }
}
